How to do it?:
Open the Rmarkdown file of this assignment (link) in Rstudio.
Right under each question, insert a code chunk (you can use the hotkey Ctrl + Alt + I to add a code chunk) and code the solution for the question.
Knit the rmarkdown file (hotkey: Ctrl + Alt + K) to export an html.
Publish the html file to your Githiub Page.
Submission: Submit the link on Github of the assignment to Blackboard.
gganimate and gifski then restart Rstudio. Use the WHO’s dataset at this link. Make a top-10 bar race by months between countries on the number of deaths by Covid 19.#Libraries
library(tidyverse)
library(gifski)
library(gganimate)
library(lubridate)
#Dataframe
df <- read_csv('https://covid19.who.int/WHO-COVID-19-global-data.csv')
#Filter for values less than zero
df <- df %>% filter(New_deaths>0, New_cases>0)
#Create month column
df$month <- month(df$Date_reported)
#Find average deaths by month and country
d1 <- df %>% group_by(month, Country) %>% summarise(mean = mean(Cumulative_deaths))
#Find rank of each country by month
d2 <- d1 %>% group_by(month) %>% mutate(rank=rank(-mean)) %>% ungroup()
#Only show top 10 for rank
d3 <- d2 %>% filter(rank <= 10)
#Create the plot
#Make the plot a col plot and add the labels for each country
#Setting for titles and axis labels/ticks
#Animation settings
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=Country, fill=Country, label=Country)) + geom_col()+
geom_text(aes(y = mean, label = Country), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = '# of Deaths by Country: Month {closest_state}', x='', y='Total Number of Deaths', fill='Country',caption = 'Italy and China lead in deaths, but by the now, U.S. and Brazil are in the lead')+
theme(plot.title = element_text(hjust = 1, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(month)+
ease_aes("cubic-in-out")
animate(a1, nframes = 400, fps = 10)
#Filter for values less than zero
df <- df %>% filter(New_deaths>0, New_cases>0)
#Create week column
df$week <- week(df$Date_reported)
#Find average cases by week and region
d1 <- df %>% group_by(week, WHO_region) %>% summarise(mean = mean(Cumulative_cases))
#Find rank of each region by week
d2 <- d1 %>% group_by(week) %>% mutate(rank=rank(-mean)) %>% ungroup()
#Only show top 10 for rank
d3 <- d2 %>% filter(rank <= 10)
#Create the plot
#Make the plot a col plot and add the labels for each country
#Setting for titles and axis labels/ticks
#Animation settings
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=WHO_region, fill=WHO_region, label=WHO_region)) + geom_col()+
geom_text(aes(y = mean, label = WHO_region), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = '# of Positive Cases by Region :Week {closest_state}', x='', y='Total Number of Positive Caeses', fill='WHO_region',caption = 'WPRO region was in the lead because of China but now SEARO is in the lead with India')+
theme(plot.title = element_text(hjust = 1, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(week)+
ease_aes("cubic-in-out")
animate(a1, nframes = 400, fps = 10)
df = read_csv('https://covidtracking.com/data/download/all-states-history.csv')
#Filter for values less than zero
df <- df %>% filter(hospitalizedCumulative>0)
#Create week column
df$week <- week(df$date)
#Find average increased hospitalizations by week and state
d1 <- df %>% group_by(week, state) %>% summarise(mean = mean(hospitalizedCumulative))
#Find rank of each state by week
d2 <- d1 %>% group_by(week) %>% mutate(rank=rank(-mean)) %>% ungroup()
#Only show top 10 for rank
d3 <- d2 %>% filter(rank <= 10)
#Create the plot
#Make the plot a col plot and add the labels for each country
#Setting for titles and axis labels/ticks
#Animation settings
a1 <- d3 %>% ggplot(aes(x=rank, y=mean, group=state, fill=state, label=state)) + geom_col()+
geom_text(aes(y = mean, label = state), hjust = 1.4)+
coord_flip(clip = "off", expand = FALSE) +scale_x_reverse()+
labs(title = 'Incre. Hospitalizations by state :Week {closest_state}', x='', y='Total Number of Hospitalizations', fill='state',caption = 'New York still leads Hospitalizations but Florida and Georgia are on the rise')+
theme(plot.title = element_text(hjust = 1, size = 22),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
transition_states(week)+
ease_aes("cubic-in-out")
animate(a1, nframes = 400, fps = 10)